﻿// Decompiled with JetBrains decompiler
// Type: Microsoft.InfoCards.GetPolicyDetailsRequest
// Assembly: infocard, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
// MVID: 8E14765A-6610-409A-BA36-099A0642905D
// Assembly location: E:\git\ALLIDA\windll\infocard.exe

using Microsoft.InfoCards.Diagnostics;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Microsoft.InfoCards
{
  internal class GetPolicyDetailsRequest : UIAgentRequest
  {
    private string m_privacyPolicyLink = string.Empty;
    private InfoCardPolicy m_policy;
    private Recipient m_recipient;

    public GetPolicyDetailsRequest(
      IntPtr rpcHandle,
      Stream inArgs,
      Stream outArgs,
      ClientUIRequest parent)
      : base(rpcHandle, inArgs, outArgs, parent)
    {
    }

    protected override void OnInitializeAsSystem()
    {
      base.OnInitializeAsSystem();
      this.m_policy = this.GetPolicy();
    }

    protected override void OnMarshalInArgs()
    {
    }

    protected override void OnProcess()
    {
      InfoCardTrace.Assert(null != this.m_policy, "Null policy");
      StoreConnection connection = StoreConnection.GetConnection();
      try
      {
        DataRow singleRow = connection.GetSingleRow(new List<QueryParameter>()
        {
          new QueryParameter("ix_objecttype", new object[1]
          {
            (object) -3
          }),
          new QueryParameter("ix_name", new object[1]
          {
            (object) this.m_policy.Recipient.GetIdentifier()
          })
        }.ToArray());
        X509RecipientIdentity recipient = this.m_policy.Recipient as X509RecipientIdentity;
        if (recipient != null)
          this.ParentRequest.CertCacheAdd(this.m_policy.Recipient.GetIdentifier(), recipient.LeafCertificate);
        if (singleRow != null)
        {
          this.m_recipient = new Recipient((Stream) new MemoryStream(singleRow.GetDataField()), this.m_policy.Recipient, true);
          if (this.m_recipient.Trust == Recipient.TrustDecision.IsTrusted && (int) this.m_recipient.PrivacyPolicyVersion != (int) this.m_policy.PrivacyPolicyVersion)
            this.m_recipient.Trust = Recipient.TrustDecision.PolicyVersionChange;
          this.m_recipient.PrivacyPolicyVersion = this.m_policy.PrivacyPolicyVersion;
        }
        else
        {
          this.m_recipient = new Recipient(this.m_policy.Recipient, true, this.m_policy.PrivacyPolicyVersion);
          this.m_recipient.Trust = Recipient.TrustDecision.NoTrustDecision;
        }
        this.m_privacyPolicyLink = this.m_policy.PrivacyPolicyLink;
      }
      finally
      {
        connection.Close();
      }
    }

    protected override void OnMarshalOutArgs()
    {
      InfoCardTrace.Assert(null != this.m_policy, "Null policy");
      BinaryWriter writer = new BinaryWriter(this.OutArgs, Encoding.Unicode);
      writer.Write(this.m_policy.RequiresManagedCard);
      writer.Write(this.m_policy.RequiresSelfIssuedCard);
      writer.Write(this.ParentRequest is GetBrowserTokenRequest);
      this.m_recipient.Serialize(writer);
      Utility.SerializeString(writer, this.m_privacyPolicyLink);
      if (this.m_policy.RequiredClaims != null)
      {
        writer.Write(this.m_policy.RequiredClaims.Length);
        for (int index = 0; index < this.m_policy.RequiredClaims.Length; ++index)
          Utility.SerializeString(writer, this.m_policy.RequiredClaims[index]);
      }
      else
        writer.Write(0);
      if (this.m_policy.OptionalClaims != null)
      {
        writer.Write(this.m_policy.OptionalClaims.Length);
        for (int index = 0; index < this.m_policy.OptionalClaims.Length; ++index)
          Utility.SerializeString(writer, this.m_policy.OptionalClaims[index]);
      }
      else
        writer.Write(0);
    }
  }
}
